package com.qianfeng.tiantian_banjia.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.os.Environment;
import android.os.StatFs;
import android.util.Log;

/**
 * 璁剧疆闈欐�鏂规硶锛岄�杩囩被灏辩洿鎺ヨ皟鐢�
 */
public class SDCardHelper {
	private static final String TAG = "SDCardHelper";

	/**
	 * 鍒ゆ柇SDCard鐨勫姞杞芥儏鍐�
	 * 
	 * @return true 浠ｈ〃鎸傝浇鎴愬姛锛宖alse 澶辫触
	 */
	public static boolean isMounted() {
		// 鐘舵�杩斿洖浼氱殑鏄瓧绗︿覆
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			Log.i("123", "1111111111111111");
			return true;
		}
		Log.i("123", "222222222222222");
		return false;
	}

	/**
	 * 鑾峰彇SDCard鐨勬牴璺緞锛團ile璺緞锛�
	 * 
	 * @return file涓嶄负null璇存槑鑾峰彇璺緞鎴愬姛锛屽惁鍒欏け璐�
	 */
	public static File getSDCardRootPath() {
		if (isMounted()) {
			// 鍔犺浇鎯呭喌ok鍙互鑾峰彇璺緞
			return Environment.getExternalStorageDirectory();
		}
		return null;
	}

	/**
	 * 鑾峰彇 SDCard鐨勫叕鍏辫矾寰�
	 * 
	 * @return file涓嶄负null璇存槑鑾峰彇璺緞鎴愬姛锛屽惁鍒欏け璐�
	 * 
	 */
	public static File getSDCardPublicPath(String type) {
		if (isMounted()) {
			return Environment.getExternalStoragePublicDirectory(type);
		}
		return null;
	}

	/**
	 * 鑾峰彇SDCard鎬荤殑瀹归噺澶у皬
	 */
	public static long getSDCardTotalSize() {
		if (isMounted()) { // 杩斿洖 long绫诲瀷鐨勫繀椤绘渶浣庣増鏈�8浠ヤ笂
			StatFs statFs = new StatFs(getSDCardRootPath().getAbsolutePath());
			// 鑾峰彇size澶у皬(绯荤粺鏈�綆鐗堟湰18浠ヤ笂)
			long size = statFs.getBlockSize();
			// 鑾峰彇block澶у皬
			long block = statFs.getBlockCount();
			// 杩斿洖鍏ㄩ儴鐨勫ぇ灏�B-->KB-->MB-->GB) /1024杞崟浣�
			return size * block / 1024 / 1024;
		}
		return 0;
	}

	/**
	 * 鑾峰彇SDCard鐨勫彲鐢ㄧ┖闂村ぇ灏�
	 */
	public static long getSDCardAvailableSize() {
		if (isMounted()) {
			StatFs statFs = new StatFs(getSDCardRootPath().getAbsolutePath());
			// size鐨勫ぇ灏�
			long size = statFs.getBlockSize();
			// 鍙敤鐨刡lock鏁伴噺
			long block = statFs.getAvailableBlocks();
			return size * block / 1024 / 1024;
		}
		return 0;
	}

	/**
	 * 鍐欏叆鍐呭鍒癝DCard涓�
	 * 
	 * @param dirName
	 *            鐩綍鐨勫悕绉�
	 * @param fileName
	 *            鐩綍涓枃浠剁殑鍚嶅瓧
	 * @param data
	 *            鍐欏叆鐨勫疄闄呭唴瀹�
	 * @return true浠ｈ〃鍐欏叆鎴愬姛锛宖alse鍐欏叆澶辫触
	 */
	public static boolean writeToSDCard(String dirName, String fileName,
			byte[] data) {
		String fileRootPath = getSDCardRootPath().getAbsolutePath();
		// 閲嶆柊鎷兼帴鏂扮殑璺緞
		String newFilePath = fileRootPath + File.separator + dirName;
		File fileDir = new File(newFilePath);
		if (!fileDir.exists()) { 
			// 濡傛灉璇ユ枃浠剁洰褰曚笉瀛樺湪锛屽垱寤烘枃浠剁洰褰�
			boolean b = fileDir.mkdirs();
			Log.i(TAG, b == true ? "鍒涘缓鎴愬姛" : "鍒涘缓澶辫触");
		}
		FileOutputStream fos = null;
		BufferedOutputStream baos = null;
		try {
			// 寮�鏂囦欢鐨勮鍐�灏唂ileDir+fileName)
			fos = new FileOutputStream(new File(fileDir, fileName));
			baos = new BufferedOutputStream(fos);
			baos.write(data);
			baos.flush();
			return true;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (baos != null)
					baos.close();
				if (fos != null)
					fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return false;
	}

	/**
	 * 浠嶴DCard涓鍙栧唴瀹�
	 * 
	 * @param filePath
	 *            璇诲彇鏂囦欢鐨勮矾寰�
	 * @return 杩斿洖璇诲彇鐨勫唴瀹� null琛ㄧず璇诲彇鍐呭澶辫触
	 */
	public static byte[] readFromSDCard(String filePath) {
		Log.i("123", "sdcard root:"+getSDCardRootPath().getAbsolutePath());
		String newFilePath = getSDCardRootPath().getAbsolutePath()
				+ File.separator + filePath;
		Log.i("123", "newFilePath:"+newFilePath);
		// 寮�璇诲彇
		FileInputStream fis = null;
		BufferedInputStream bis = null;
		ByteArrayOutputStream baos = null;
		try {
			fis = new FileInputStream(new File(newFilePath));
			bis = new BufferedInputStream(fis);
			baos = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = bis.read(buffer)) != -1) {
				baos.write(buffer, 0, len);
			}
			return baos.toByteArray();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (baos != null)
					baos.close();
				if (bis != null)
					bis.close();
				if (fis != null)
					fis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}

}
